from bs4 import BeautifulSoup
import requests

url = 'https://news.baidu.com/'
ua = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36'
}
req = requests.get(url, headers=ua)
# print(req.content.decode('utf-8'))

html = req.content.decode('utf-8') # 一串字符串
# 使用beautifulSoup库解析网页
soup = BeautifulSoup(html, 'lxml')
# soup.prettify() 返回的是html/xml网页对象
# print("输出格式化的beautifuSoup对象：", soup.prettify())

# xpath获取到title标签：/html/head/title
# bs4获取title标签
# print("获取title标签：", soup.title)
# 获取head标签
# print("获取head标签：", soup.head)

# 获取body标签中的第一个a标签
# print("获取第一个a标签：", soup.body.a)
# 获取所有名称为a的标签 []列表 ()元组 set集合()
# print("所有名称为a的标签：", soup.find_all('a'))
# 获取所有名称为a的标签的个数
# print("所有名称为a的标签的个数：", len(soup.find_all('a')))

# 获取soup的name
# print("soup的name：", soup.name)
# 获取a标签的name
# print("a标签的name：", soup.a.name)

# 获取tag的name
# tag = soup.a
# print("tag的name：", tag.name)
# print("tag的内容：", tag)
# 修改tag的name
# tag.name = 'b'
# print("修改name后tag的内容：", tag) # 查看修改name后的html

# 获取tag对象的全部属性
# print("Tag对象的全部属性：", tag.attrs)
# 获取href属性的值
# print("href属性的值：", tag['href'])

# 修改href属性的值
# tag['href'] = "https://wandou.pro/"
# print("修改后tag对象的属性：", tag.attrs)

# 新增属性 id，赋值为pagename
# tag['id'] = "pagename"
# print("修改后tag的内容：", tag)

# 删除 data-path 属性
# del tag['data-path']
# print("修改后tag的内容：", tag)

# 获取tag对象中包含的字符串 xpath: /html/head/title/text()
tag = soup.title
print("Tag对象中包含的字符串：", tag.string)
# 查看类型
print("tag.string的类型：", type(tag.string))

print("tag替换前：", tag)
# 替换字符串内容
tag.string.replace_with("豌豆PRO")
print("tag替换后：", tag)


# 查看soup的类型
print("soup的类型：", type(soup))
# BeautifulSoup对象的特殊属性name
print("BeautifulSoup对象的特殊属性name：", soup.name)
print("soup.name的类型:", type(soup.name))
# BeautifulSoup对象的attribute属性  为空
print("BeautifulSoup对象的attribute属性:", soup.attrs)









